﻿using System;
using System.Collections.Generic;
using System.Linq;

class GradeManager
{
    private static Dictionary<string, List<double>> students = new Dictionary<string, List<double>>();

    static void AddStudent(string name)
    {
        if (students.ContainsKey(name))
        {
            Console.WriteLine($"学生{name}已存在！");
        }
        else
        {
            students[name] = new List<double>();
            Console.WriteLine($"学生{name}添加成功！");
        }
    }

    static void InputScores(string name)
    {
        if (!students.ContainsKey(name))
        {
            Console.WriteLine($"学生{name}不存在，请先添加！");
            return;
        }

        Console.Write($"请输入{name}的各科成绩（用空格分隔）: ");
        string[] input = Console.ReadLine().Split();

        try
        {
            var scores = input.Select(double.Parse).ToList();
            students[name].AddRange(scores);
            Console.WriteLine("成绩录入成功！");
        }
        catch (FormatException)
        {
            Console.WriteLine("输入错误，请确保输入的是数字！");
        }
    }

    static double CalculateAverage(List<double> scores)
    {
        return scores.Count > 0 ? scores.Average() : 0;
    }

    static void ShowAll()
    {
        Console.WriteLine("\n{0,-10}{1,-25}{2,-10}{3,-10}", "姓名", "各科成绩", "总分", "平均分");
        foreach (var student in students)
        {
            double total = student.Value.Sum();
            double avg = CalculateAverage(student.Value);
            Console.WriteLine("{0,-10}{1,-25}{2,-10:0.0}{3,-10:0.00}",
                student.Key,
                string.Join(" ", student.Value),
                total,
                avg);
        }
        Console.WriteLine();
    }

    static void Main()
    {
        while (true)
        {
            Console.WriteLine("\n学生成绩管理系统");
            Console.WriteLine("1. 添加学生");
            Console.WriteLine("2. 录入成绩");
            Console.WriteLine("3. 显示所有学生信息");
            Console.WriteLine("4. 退出系统");

            Console.Write("请输入选项（1-4）: ");
            string choice = Console.ReadLine();

            switch (choice)
            {
                case "1":
                    Console.Write("请输入学生姓名: ");
                    AddStudent(Console.ReadLine().Trim());
                    break;
                case "2":
                    Console.Write("请输入学生姓名: ");
                    InputScores(Console.ReadLine().Trim());
                    break;
                case "3":
                    ShowAll();
                    break;
                case "4":
                    Console.WriteLine("感谢使用，再见！");
                    return;
                default:
                    Console.WriteLine("无效的输入，请重新选择！");
                    break;
            }
        }
    }
}